package DataBases;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.StringTokenizer;
import java.util.Vector;

import wwtp.ontology.Water;

public class DBWaterTreatment {

	private static String path = ".\\src\\DataBases\\WaterTreatment.txt";
	private static DBWaterTreatment instance = null;
	private VectorTreatment dB;
	private Vector<Integer> chemicalLimits,biologicalLimits,physicalLimits;

	private DBWaterTreatment(){
		dB = new VectorTreatment();
		chemicalLimits = new Vector<Integer>();
		biologicalLimits = new Vector<Integer>();
		physicalLimits = new Vector<Integer>();
		init();
	}

	private void init(){
		WaterTreatmentObject item;
		StringTokenizer token;
		Propiedades prop;
		int[] t = new int[8];
		double physical, chemical, biological;

		BufferedReader reader;

		try {
			reader = new BufferedReader(new FileReader(new File(path)));
			String line = null;
			token = new StringTokenizer(reader.readLine(),";");
			while(token.hasMoreTokens()){
				physicalLimits.add(new Integer(token.nextToken()));
			}
			token = new StringTokenizer(reader.readLine(),";");
			while(token.hasMoreTokens()){
				biologicalLimits.add(new Integer(token.nextToken()));
			}
			token = new StringTokenizer(reader.readLine(),";");
			while(token.hasMoreTokens()){
				chemicalLimits.add(new Integer(token.nextToken()));
			}			
			
			while((line = reader.readLine()) != null){
				token = new StringTokenizer(line,";");
				physical = new Double(token.nextToken());
				biological = new Double(token.nextToken());	
				chemical = new Double(token.nextToken());							
				
				prop = new Propiedades(physical,biological,chemical);
				for (int i = 0; i < 4; i++) {
					t[i] = new Integer(token.nextToken());
				}
				item = new WaterTreatmentObject(prop,t[0],t[1],t[2],t[3]);
				dB.add(item);
			}
			
			//dB.print();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static DBWaterTreatment getInstance(){
		if(instance==null){
			instance = new DBWaterTreatment();
		}
		return instance;
	}

	public WaterTreatmentObject getTreatment(Water w) {
		
		//System.out.println(w.getPhysical().getPercentaje() + " " + w.getChemical().getPercentaje() + " " +w.getBiological().getPercentaje());
		double phy = physicalUpperLimit(w.getPhysical().getPercentaje());
		double che = chemicalUpperLimit(w.getChemical().getPercentaje());
		double bio = biologicalUpperLimit(w.getBiological().getPercentaje());
		Propiedades prop = new Propiedades(phy,bio,che);
		
		
		float litres = w.getVolume().getLitres();		

		return dB.treatment(litres, prop);
	}

	private double physicalUpperLimit(double d) {
		
		double ret = 0;
		for (int i = 0; i < physicalLimits.size(); i++) {
			if (physicalLimits.get(i) >= d) {
				return physicalLimits.get(i);
			}
		}
		return ret;
	}
	
	private double chemicalUpperLimit(double d) {
		
		double ret = 0;
		for (int i = 0; i < chemicalLimits.size(); i++) {
			if (chemicalLimits.get(i) >= d) {
				return chemicalLimits.get(i);
			}
		}
		return ret;

	}
	
	private double biologicalUpperLimit(double d) {
		
		double ret = 0;
		for (int i = 0; i < biologicalLimits.size(); i++) {
			if (biologicalLimits.get(i) >= d) {
				return biologicalLimits.get(i);
			}
		}
		return ret;

	}
}
